#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <string>
#include <iostream>
#include <fcntl.h>
#include "../md5/md5.h"

using namespace utils;
using namespace boost::filesystem;

class TestMD5Stream : public testing::Test
{
public:
    void SetUp()
    {
        p = "md5testfile.txt";
        if (exists(p))
        {
            remove(p);
        }
        m_file_stream.open(p, std::ios::out);

        m_file_stream << "test string";
        m_file_stream.close();
    };

    void TearDown()
    {
        if (exists(p))
        {
            remove(p);
        }
    };

    path p;
    fstream m_file_stream;
};

TEST_F(TestMD5Stream, md5_file_stream)
{
    char buf[MD5_DIGEST_SIZE];
    char md5[MD5_BLOCK_SIZE];
    FILE* test_file = fopen(p.filename().c_str(), "r");
    md5_stream(test_file, buf);
    md5_to_string(buf, md5);
    fclose(test_file);
    EXPECT_STREQ("6f8db599de986fab7a21625b7916589c", md5);
}

TEST(md5test, buffer_test)
{
    std::string str;
    str = "test string";
    char buf[MD5_DIGEST_SIZE];
    char md5[MD5_BLOCK_SIZE];
    md5_buffer(str.c_str(), str.length(), buf);
    md5_to_string(buf, md5);
    EXPECT_STREQ("6f8db599de986fab7a21625b7916589c", md5);
}